$(function(){
    var pagenum =10;
    var fi =1;
	var page=1;
	var tddata=[
		{
			key:'company_name',
			type:1,
			url:''
		},{
			key:'short_name',
			type:1,
			url:'{:url("detail")}',
			id:137
		},{
			key:'code',
			type:1,
			url:''
		},{
			key:'account',
			type:1,
			url:''
		},{
			key:'linkman',
			type:1,
			url:''
		},{
			key:'phone',
			type:1,
			url:''
		},{
			key:'address',
			type:1,
			url:''
		},{
			key:'law_type2',
			type:1,
			url:''
		}
		];
	
	var addoperation=``;
	var fields='';
	
	var data={
		page:1,
		pagenum:10
	}
	
	var where_json={}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	
	
	data.where_json=JSON.stringify(where_json)
	ajaxcompany(data)
	function ajaxcompany(data){
		Load()
		$.ajax({
			type:'post',
			data:data,
			url:api.company.list,
			success:function(res){
				Load(1)
				console.log(res)
				if(res.code==1){
				for(var i=0;i<res.data.length;i++){
					if(res.data[i].law_type==1){
						res.data[i].law_type2='非行政执法'
					}else{
						res.data[i].law_type='行政执法'
					}
					
					
					 
				}	  
			  //展示已知数据
			 	 table.render({
			    	elem: '#company'
				    ,cols: [[ //标题栏
				      {field: 'company_name', title: '公司名称', width: 250, sort: true,fixed: 'left'}
				      ,{field: 'short_name', title: '公司简称', width: 150}
				      ,{field: 'code', title: '公司代码', minWidth: 150}
				      ,{field: 'account', title: '登陆账号', minWidth: 150}
				      ,{field: 'linkman', title: '联系人', width: 120}
				      ,{field: 'phone', title: '联系电话', minWidth: 150}
				      ,{field: 'address', title: '地址', minWidth: 150}
				      ,{field: 'law_type2', title: '行政执法', width: 150}
				     
				    ]]
				    ,data:res.data
				    ,limit: data.pagenum
				    ,even: true
			
			  	});
				//   分页
                if (fi == 1) {
                    laypage.render({
                        elem: 'page',
                        count: res.count,
						layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
						theme: '#1E9FFF',
                        jump: function (obj, first) {
                            console.log(obj)
                            if (!first) {
                                page = obj.curr;
                                data.page = page;
                                pagenum = obj.limit;
                                data.pagenum = pagenum;
                                ajaxcompany(data);
                            } else {
        
                            }
                        }
					})
					$('.daochu').on('click',function(){
						var getkey="";
						for(var iii=0;iii<tddata.length;iii++){
							if(tddata[iii].key=='law_type2'){
								for(var key in res.description){
									if(key=='law_type'){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='law_type' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}else{
								for(var key in res.description){
									if(key==tddata[iii].key){
										tddata[iii].text=res.description[key].name
									}
								}
								getkey+=`<li>
										<label class="am-checkbox">
									      <input type="checkbox" value="" data-key='${tddata[iii].key}' checked> ${tddata[iii].text}
									    </label>
									</li>`
							}
							
						}
						layer.open({
						  	type: 1,
						  	title:false,
						  	skin: 'layui-layer-rim', //加上边框
						  	area: ['300px', '400px'], //宽高
						  	content: `<div>
						 				<div style='height:300px;overflow:auto;margin-bottom:30px'>
									  	<ul class='daochuul' style="padding:10px;">${getkey}</ul>
									  	</div>
									  	<div style='text-align:center'>
                                          <button class="layui-btn layui-btn-normal daochu_ok" type='button'>确定导出</button>
                                          <button class='layui-btn layui-btn-danger am-radius' type='button'>取消导出</button>
									  	</div>
									</div>`,
							success:function(){
								var ddata={}
								$('.daochu_ok').on('click',function(){
									ddata.fields="";
									var where_jsondao={};
									{if $Think.get.company_name}
										where_jsondao.company_name=['like','%{$Think.get.company_name}%']
									{/if}
									
									
									ddata.where_json=JSON.stringify(where_jsondao)
									
									$('.daochuul li input[type="checkbox"]').each(function(){
										if(this.checked){
											ddata.fields+=($(this).data('key')+",")
										}
									})
									ddata.fields=ddata.fields.slice(0,ddata.fields.length-1)
									console.log(ddata.fields)
									console.log(`${api.company.down}?where_json=${ddata.where_json}&fields=${ddata.fields}`)
									window.open(`${api.company.down}?where_json=${ddata.where_json}&fields=${ddata.fields}`)							
								})
								
							}
						});
					})
                    fi = 2;
                }       

				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
})
